﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Yugi_Gui.COntrol;
using System.Windows.Threading;
using Yugi.Client;
using Yugi_Gui.LayoutManager;

namespace Yugi_Gui
{
    /// <summary>
    /// Interaction logic for RPGLayout.xaml
    /// </summary>
    public partial class RPGLayout : UserControl
    {
        Storyboard sb1;
        Storyboard sb2;
        Storyboard sb3;
        TypePRS type1 = TypePRS.Unknown;
        TypePRS type2 = TypePRS.Unknown;
        DispatcherTimer dispatcherTimer;
        DispatcherTimer dispatcherTimer2;

        SupervisorModel super;
        Game1Layout game;
        int id;

        int OponentHand;
        int MyHand;


        public RPGLayout(SupervisorModel s, Game1Layout g ,int ii)
        {
            InitializeComponent();

            stackPanel1.IsHitTestVisible = false;

            super = s;
            game = g;
            id = ii;

            s.RPSHand += new Action<int, int>(OnRPSHand);
            s.RPSCanClick += new Action(OnCanClick);
            s.RPSChose += new Action<int, int>(OnChose);
            
            chose1.MouseEnter += new MouseEventHandler(chose1_MouseEnter);
            chose2.MouseEnter += new MouseEventHandler(chose2_MouseEnter);
            chose1.MouseLeave += new MouseEventHandler(chose1_MouseLeave);
            chose2.MouseLeave += new MouseEventHandler(chose2_MouseLeave);

            chose1.MouseLeftButtonDown += new MouseButtonEventHandler(chose1_MouseLeftButtonDown);
            chose2.MouseLeftButtonDown += new MouseButtonEventHandler(chose2_MouseLeftButtonDown);

            Canvas.SetZIndex(chose1, -10);
            Canvas.SetZIndex(chose2, -10);
            for (int i = 0; i < 20; i++)
            {
                TypePRS type=TypePRS.Scissors;
                switch (i % 3)
                {
                    case 0:
                        type = TypePRS.Paper;
                        break;
                    case 1:
                        type = TypePRS.Rock;
                        break;
                    case 2:
                        type = TypePRS.Scissors;
                        break;
                }

                SmallImg sm = new SmallImg(type,i);
               
              
                stackPanel1.Children.Add(sm);
                sm.ClickImage += new Action<SmallImg>(ClickHandle);
               // im.MouseEnter += new MouseEventHandler(im_MouseEnter);

                SmallImg sm1 = new SmallImg(TypePRS.Unknown, -1);
                stackPanel2.Children.Add(sm1);
                Canvas.SetZIndex(rec, -1);
            }

            sb1 = (Storyboard)FindResource("Storyboard1");

            sb1.Begin();
            sb2 = (Storyboard)FindResource("Storyboard2");
            sb2.Begin();
        }

        

        void OnChose(int id, int c)
        {
            if (c == 1)
            {
                sb3 = (Storyboard)FindResource("Storyboard5");
                sb3.Completed += new EventHandler(sb3_Completed);
                sb3.Begin();
                super.AllowSkip = false;
            }
            else
            {
                sb3 = (Storyboard)FindResource("Storyboard4");
                sb3.Completed += new EventHandler(sb3_Completed);
                sb3.Begin();
                super.AllowSkip = true;
            }
           
        }

        void sb3_Completed(object sender, EventArgs e)
        {
            super.SendMessageToServer(Common.YMSG.RPS_Finish);
            LayoutManager.LayoutManager.Insitance.LoadExistLayout(game);
        }

        void OnCanClick()
        {
            stackPanel1.IsHitTestVisible = true;
        }

        void OnRPSHand(int id, int h)
        {
            if (h == 0)
                type2 = TypePRS.Paper;
            else
                if (h == 1)
                    type2 = TypePRS.Rock;
                else
                    type2 = TypePRS.Scissors;
            if (type1 != null)
                Chose();
        }
        void chose1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            layoutRoot.IsHitTestVisible = false;
            super.SendMessageToServer(Common.YMSG.RPS_Chose, id, 1);
            LayoutManager.LayoutManager.Insitance.LoadExistLayout(game);
            super.AllowSkip = true;
        }

        void chose2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            layoutRoot.IsHitTestVisible = false;
            super.SendMessageToServer(Common.YMSG.RPS_Chose, id, 2);
            LayoutManager.LayoutManager.Insitance.LoadExistLayout(game);
            super.AllowSkip = false;
        }

        void chose2_MouseLeave(object sender, MouseEventArgs e)
        {
            chose2.Opacity = 0;
        }

        void chose1_MouseLeave(object sender, MouseEventArgs e)
        {
            chose1.Opacity = 0;
        }

        void chose2_MouseEnter(object sender, MouseEventArgs e)
        {
            chose2.Opacity = 100;
        }

        void chose1_MouseEnter(object sender, MouseEventArgs e)
        {
            chose1.Opacity = 100;
        }

        void ClickHandle(SmallImg sm)
        {
            MyHand = sm.index;
            type1 = sm.type;
            stackPanel1.IsHitTestVisible = false;

            int c = 0;
            switch (type1)
            {
                case TypePRS.Paper:
                    c = 0;
                    break;
                case TypePRS.Rock:
                    c = 1;
                    break;
                case TypePRS.Scissors:
                    c = 2;
                    break;
            }
            super.SendMessageToServer(Common.YMSG.RPS_Hand, id, c);

            SmallImg small = (SmallImg)stackPanel1.Children[MyHand];
            small.HighlightState();
            small.LostMouse();

            DoubleAnimationUsingKeyFrames db = (DoubleAnimationUsingKeyFrames)sb1.Children[0];

            double x = Canvas.GetLeft(stackPanel1);
            double center = 360- (sm.index * 255 + x);

            db.KeyFrames[0].Value = Canvas.GetLeft(stackPanel1);
            db.KeyFrames[0].KeyTime = new TimeSpan(0);

            db.KeyFrames[1].Value = Canvas.GetLeft(stackPanel1) + center;
            db.KeyFrames[1].KeyTime = new TimeSpan(0, 0, 0, 0, 300);


            sb1.Stop();
            sb1.Begin();

            dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
            dispatcherTimer2.Interval = new TimeSpan(0, 0, 0, 0, 800);
            dispatcherTimer2.Start();

            
        }

        void dispatcherTimer2_Tick(object sender, EventArgs e)
        {
            dispatcherTimer2.Stop();
            if (type1 != TypePRS.Unknown && type2 != TypePRS.Unknown)
                ShowHand(type1, type2);
        }

        private void Chose()
        {
            Random r = new Random();
            OponentHand = r.Next(5, 15); ;
            
            //sb2.Completed += new EventHandler(sb_Completed);
            
            DoubleAnimationUsingKeyFrames db = (DoubleAnimationUsingKeyFrames)sb2.Children[0];

            double x = Canvas.GetLeft(stackPanel2);
            double center = 360 - (OponentHand * 255 + x);

            db.KeyFrames[0].Value = Canvas.GetLeft(stackPanel2);
            db.KeyFrames[0].KeyTime = new TimeSpan(0);

            db.KeyFrames[1].Value = Canvas.GetLeft(stackPanel2) + center;
            db.KeyFrames[1].KeyTime = new TimeSpan(0, 0, 0, 0, 300);
            sb2.Stop();
            sb2.Begin();


            
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer1_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);       
            dispatcherTimer.Start();

            SmallImg im = (SmallImg)stackPanel2.Children[OponentHand];
            im.img.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/o2.jpg"));    
        }


        void dispatcherTimer1_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Stop();

            if (type1 != TypePRS.Unknown && type2 != TypePRS.Unknown)
                ShowHand(type1, type2);
        }
       

        private void ShowHand(TypePRS t1,TypePRS t2)
        {
            type1 = t1;
            type2 = t2;
            rec.Opacity = 100;
            Canvas.SetZIndex(rec, 0);
            switch (t1)
            {
                case TypePRS.Paper:
                    hand1.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/p3.jpg"));
                    break;
                case TypePRS.Rock:
                    hand1.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/r3.jpg"));
                    break;
                case TypePRS.Scissors:
                    hand1.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/s3.jpg"));
                    break;
            }

            switch (t2)
            {
                case TypePRS.Paper:
                    hand2.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/p3.jpg"));
                    break;
                case TypePRS.Rock:
                    hand2.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/r3.jpg"));
                    break;
                case TypePRS.Scissors:
                    hand2.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/s3.jpg"));
                    break;
            }
            
            
            sb3 = (Storyboard)FindResource("Storyboard3");
           // sb.Completed+=new EventHandler(sb3_Completed);
            sb3.Begin();

          
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 400);        
            dispatcherTimer.Start();

        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Stop();
            sb3.Stop();
            int test = HandDuel(type1, type2);

            rec.Opacity = 0;
            Canvas.SetZIndex(rec, -1);
            hand1.Source = hand2.Source = null;
            if (test == 0)
            {
                type1 = type2 = TypePRS.Unknown;

                Canvas.SetTop(hand1, 60);
                Canvas.SetTop(hand2, 405);
                
               
                DoubleAnimationUsingKeyFrames db = (DoubleAnimationUsingKeyFrames)sb1.Children[0];

                db.KeyFrames[0].Value = -1000;
                db.KeyFrames[0].KeyTime = new TimeSpan(0);
                db.KeyFrames[1].Value = -2000;
                db.KeyFrames[1].KeyTime = new TimeSpan(0, 0, 30);

                db = (DoubleAnimationUsingKeyFrames)sb2.Children[0];
                db.KeyFrames[0].Value = -4000;
                db.KeyFrames[0].KeyTime = new TimeSpan(0);
                db.KeyFrames[1].Value = -3000;
                db.KeyFrames[1].KeyTime = new TimeSpan(0, 0, 30);

                Canvas.SetLeft(stackPanel1, -1000);
                Canvas.SetLeft(stackPanel2, -4000);
                sb1.Begin();
                sb2.Begin();

                stackPanel1.IsHitTestVisible = true;

                SmallImg im = (SmallImg)stackPanel2.Children[OponentHand];
                im.img.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/o1.jpg"));

                SmallImg im1 = (SmallImg)stackPanel1.Children[MyHand];
                im1.NormalState();
                im1.GotMouse();
                return;
            }

            main.Source = new BitmapImage(new Uri("pack://application:,,,/Image/RPG/main2.jpg"));
            stackPanel1.Opacity = 0;
            stackPanel2.Opacity = 0;
            Canvas.SetZIndex(chose1, 10);
            Canvas.SetZIndex(chose2, 10);
            Canvas.SetZIndex(stackPanel1, -1);
            Canvas.SetZIndex(stackPanel2, -1);
            if (test == 1)
            {
                
            }

            if (test == -1)
            {
                layoutRoot.IsHitTestVisible = false;
            }
        }

       
        int HandDuel(TypePRS t1, TypePRS t2)
        {
            if (t1 == TypePRS.Paper)
                if (t2 == TypePRS.Paper)
                    return 0;
                else
                    if (t2 == TypePRS.Rock)
                        return 1;
                    else
                        return -1;
            else
                if (t1 == TypePRS.Rock)
                    if (t2 == TypePRS.Paper)
                        return -1;
                    else
                        if (t2 == TypePRS.Rock)
                            return 0;
                        else
                            return 1;
                else
                    if (t2 == TypePRS.Paper)
                        return 1;
                    else
                        if (t2 == TypePRS.Rock)
                            return -1;
                        else
                            return 0;
        }
        void im_MouseEnter(object sender, MouseEventArgs e)
        {
            Image im = (Image) sender;
            
        }
    }
}
